// 缓存数据字典数据
class Dictionary {
  // 私有变量
  private cache = new Map()

  /**
   * 获取缓存字典数据
   * @string key 缓存键
   * @function fn 缓存值调用接口
   * @param args 接口调用参数
   * @returns {Promise<{}>}
   */
  public async get(key: string, fn: (...args: any) => void, ...args: string[]) {
    const { cache } = this
    if (cache.get(key)) {
      return cache.get(key)
    }
    const res = await fn.apply(this, args)
    cache.set(key, res)
    return res
  }

  /**
   * 强制更新字典数据
   * @string key 缓存键
   * @function fn 缓存值调用接口
   * @param args 接口调用参数
   * @returns {Promise<{}>}
   */
  public async forceUpdate(
    key: string,
    fn: (...args: any) => void,
    ...args: string[]
  ) {
    const res = await fn.apply(this, args)
    this.cache.set(key, res)
    return res
  }

  /**
   * 清除所以数据
   */
  public clear() {
    this.cache.clear()
  }
}

const dictionary = new Dictionary()
Object.freeze(dictionary)

export const getDictList = async (keys: string[]) => {
  const promises: Promise<{ [key: string]: any }>[] = []
  keys.forEach((item) => {
    // return promises.push(dictionary.get(item, getVal));
    //   promises.push(
    //     new Promise((resolve) => {
    //       setTimeout(() => {
    //         console.log(item);
    //         resolve(dictionary.get(item, getVal));
    //       }, 1000);
    //     })
    //   );
    promises.push(dictionary.get(item, () => {}))
  })

  const res = await Promise.allSettled(promises)
  const data = res.map((item) =>
    item.status === "fulfilled" ? item.value : []
  )
  return data
}
